package com.cyb.myfriend.exception;

import com.cyb.myfriend.constant.BaseResponse;
import com.cyb.myfriend.constant.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author cyb
 * @date 2024/08/12 08:55
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        log.info("BusinessException: " + e.getMessage(), e);
        return new BaseResponse<>(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.info("RuntimeException: " + e.getMessage(), e);
        return new BaseResponse<>(StatusCode.SYSTEM_ERROR.getCode(), e.getMessage());
    }
}

